﻿var xmlhttp;

function CreateRequest() {
    try {
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    catch (failed) {
        xmlhttp = null;
    }

    return xmlhttp;
}

//if (xmlhttp == null) {
//    alert("Error:Creating request object.");
//}

//var   result   =   new   Array(new   Array("1","1111"),      
//        new   Array("2","2222"),   new   Array("3","3333"),      
//        new   Array("4","4444"));      
//  for(var   i=0;i<   result.length;i++){      
//  alert(result[i][0]+"|"+result[i][1]);      
// }      


//待修改
function createXML(operationFlag, tableName, arrData, arrWhere) {
    var xml = "<PartInfo>";
    xml += "<Flag>" + operationFlag + "</Flag>";
    xml += "<Table>" + tableName + "</Table>";


    if (arrData != null) {
        xml += "<Data>";
        for (var i = 0; i < arrData.length; i++) {
            xml = xml + '<' + arrData[i][0] + '>' + arrData[i][1] + '</' + arrData[i][0] + '>';
        }
        xml += "</Data>";
    }


    if (arrWhere != null) {

        xml += "<Where>";
        for (var i = 0; i < arrWhere.length; i++) {
            xml = xml + '<' + arrWhere[i][0] + '>' + arrWhere[i][1] + '</' + arrWhere[i][0] + '>';
        }
        xml += "</Where>";
    }

    xml += "</PartInfo>";
    return xml;
}


//function ProcessData(operationFlag, tableName, arrData, arrConditions) {
//    var url = "AjaxProcessData.aspx";
//    ProcessData(url, operationFlag, tableName, arrData, arrConditions)
//}

//url 后台操作页面
//operationFlag 要操作的类型标识。例如添加为"0",修改为"1",删除为"2"
//tableName 要操作的表名
//arrData 需要保存的数据，如果为删除可以为空，这是一个二维数组组成的数组
//arrConditions sql语句中where子句中的条件，这是一个二维数组组成的数组
function ProcessData(operationFlag, tableName, arrData, arrWhere) {
    xmlhttp= CreateRequest();
    var xml = createXML(operationFlag, tableName, arrData, arrWhere);
    var responseXML;
    if (xmlhttp != null) {

        xmlhttp.open("Post", "/AjaxProcessData.aspx", false);
        //"txt/xml
        xmlhttp.setRequestHeader("Content-Type", "application/x - www - form - urlencoded;"); // 
        xmlhttp.send(xml);
        return responseXML = xmlhttp.responseXML;
    }
    else {
        alert("您的浏览器不支持XMLHTTP.");
    }

}


//function UpdatePage()
//{
//    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//        var responseXML = xmlhttp.responseXML;
//        return responseXML;
////        if (responseXML.parseError.errorCode == 0) {
////            var currNode = responseXML.selectSingleNode("//SuccessFlag"); //获取单一节点（这里就是xpath的例子）
////            return currNode.text;
//        } 
//        else {
//            //判断是否出错
//            var error = responseXML.parseError;
//            alert(error.reason);
//        }
//    }
//    else {
////        var message = xmlhttp.getResponseHeader("Status");
////        if ((message.lenth == null) || (message.lenght <= 0)) {
////            alert("Error!Request status is " + xmlhttp.status);
////        } else {
////            alert(message);
////        }
//    }
//}




